

import javax.mail.*;
import javax.mail.internet.*;

import java.util.Properties;
import javax.activation.*;

public class Messaging {
	/**
	 *
	 * @param host
	 *            String
	 * @param from
	 *            String
	 * @param to
	 *            String
	 * @param sub
	 *            String
	 * @param body
	 *            String
	 * @param realfilename
	 *            String: physically (directoryname + filename)
	 * @param filenamefordisplay
	 *            String: just for display
	 * @throws AddressException
	 * @throws MessagingException
	 */
	public static void sendMail(String host, String from, String to,
			String sub, String body, String realfilename,
			String filenamefordisplay)
			throws javax.mail.internet.AddressException,
			javax.mail.MessagingException {
		boolean debug = false;
		String displayname = filenamefordisplay;
		if (System.getProperty("debug") != null)
			debug = true;
		if (filenamefordisplay == null)
			displayname = realfilename;

		// Get system properties
		Properties sysProps = System.getProperties();

		// Setup mail server
		sysProps.put("mail.debug", "true");
		sysProps.put("mail.smtp.host", host);
		sysProps.put("mail.from", from);
		// sysProps.put("mail.smtp.auth","false");

		// Get session
		Session session = Session.getDefaultInstance(sysProps, null);

		// Define message
		MimeMessage message = new MimeMessage(session);

		// Set the from address
		// message.setFrom(new InternetAddress(from));
		if (debug)
			System.out.println(InternetAddress.getLocalAddress(session));
		message.setFrom();

		// Set the to address
		message.addRecipients(Message.RecipientType.TO, InternetAddress
				.parse(to));
		// Set the subject
		message.setSubject(sub);

		MimeMultipart multi = new MimeMultipart();
		BodyPart textBodyPart = new MimeBodyPart();
		textBodyPart.setText(body);
		multi.addBodyPart(textBodyPart);
		FileDataSource fds = new FileDataSource(realfilename);
		BodyPart fileBodyPart = new MimeBodyPart();
		fileBodyPart.setDataHandler(new DataHandler(fds));
		fileBodyPart.setFileName(displayname);
		multi.addBodyPart(fileBodyPart);
		message.setContent(multi);
		message.saveChanges();

		// Send message
		Transport.send(message);
	}

	public static void sendHTMLMail(String host, String from, String to,
			String sub, String body)
			throws javax.mail.internet.AddressException,
			javax.mail.MessagingException {
		boolean debug = false;
		if (System.getProperty("debug") != null)
			debug = true;

		// Get system properties
		Properties sysProps = System.getProperties();

		// Setup mail server
		// sysProps.put("mail.debug", "true");
		sysProps.put("mail.debug", "false");
		sysProps.put("mail.smtp.host", host);
		sysProps.put("mail.from", from);
		sysProps.put("mail.smtp.auth", "false");

		// Get session
		Session session = Session.getDefaultInstance(sysProps, null);

		// Define message
		MimeMessage message = new MimeMessage(session);

		// Set the from address
		// message.setFrom(new InternetAddress(from));
		if (debug)
			System.out.println(InternetAddress.getLocalAddress(session));
		message.setFrom();

		// Set the to address
		message.addRecipients(Message.RecipientType.TO, InternetAddress
				.parse(to));
		// Set the subject
		message.setSubject(sub);
		// message.setText(body);

		// //////
		// set send time
		// message.setSentDate(new Date());

		Multipart mp = new MimeMultipart();
		MimeBodyPart mbp = new MimeBodyPart();

		// set mail format as text/plain �� text/html
		mbp.setContent(body, "text/html;charset=GB2312");
		mp.addBodyPart(mbp);
		message.setContent(mp);

		// Transport transport = session.getTransport("smtp");
		// transport.connect(SMTP, USERNAME, PASSWORD);
		// transport.sendMessage(msg, msg.getAllRecipients());

		message.saveChanges();

		// Send message
		Transport.send(message);
	}

	public static void sendHTMLMail(String host, String replyTo, String to,
			String sub, String body, String from, String password)
			throws javax.mail.internet.AddressException,
			javax.mail.MessagingException {
		boolean debug = false;
		if (System.getProperty("debug") != null)
			debug = true;

		// Get system properties
		Properties sysProps = System.getProperties();

		// Setup mail server
		sysProps.put("mail.debug", "true");
		// sysProps.put("mail.debug","false");
		sysProps.put("mail.smtp.host", host);
		// sysProps.put("mail.from", from);
		sysProps.put("mail.from", replyTo);
		sysProps.put("mail.smtp.auth", "true");

		sysProps.put("mail.smtp.user", from);
		sysProps.put("mail.smtp.password", password);

		// Get session
		// Session session = Session.getDefaultInstance(sysProps, null);
		MyAuthenticator myauth = new MyAuthenticator(from, password);
		// Session session = Session.getDefaultInstance(sysProps, myauth);
		Session session = Session.getInstance(sysProps, myauth);

		// Define message
		MimeMessage message = new MimeMessage(session);
		// Set the from address
		// message.setFrom(new InternetAddress(from));
		// message.setFrom(new InternetAddress(replyTo));
		if (debug)
			System.out.println(InternetAddress.getLocalAddress(session));

		// Set the to address
		message.addRecipients(Message.RecipientType.TO, InternetAddress
				.parse(to));
		// Set the subject
		message.setSubject(sub);
		// message.setReplyTo(InternetAddress.parse(replyTo));
		// set send time
		// message.setSentDate(new Date());

		Multipart mp = new MimeMultipart();
		MimeBodyPart mbp = new MimeBodyPart();

		// set mail format as text/plain �� text/html
		mbp.setContent(body, "text/html;charset=GB2312");
		mp.addBodyPart(mbp);
		message.setContent(mp);

		message.saveChanges();
		// Send message
		Transport.send(message);
	}

	public static void sendHTMLMail(String host, String from, String to,
			String sub, String body, String realfilename,
			String filenamefordisplay, String type)
			throws javax.mail.internet.AddressException,
			javax.mail.MessagingException {
		boolean debug = false;
		if (System.getProperty("debug") != null)
			debug = true;
		String displayname = filenamefordisplay;
		if (filenamefordisplay == null)
			displayname = realfilename;

		// Get system properties
		Properties sysProps = System.getProperties();

		// Setup mail server
//		sysProps.put("mail.debug", "true");
		sysProps.put("mail.debug", "false");
		sysProps.put("mail.smtp.host", host);
		sysProps.put("mail.from", from);
		sysProps.put("mail.smtp.auth", "false");

		// Get session
		Session session = Session.getDefaultInstance(sysProps, null);

		// Define message
		MimeMessage message = new MimeMessage(session);

		// Set the from address
		if (debug)
			System.out.println(InternetAddress.getLocalAddress(session));
		message.setFrom();

		// Set the to address
		message.addRecipients(Message.RecipientType.TO, InternetAddress
				.parse(to));
		// Set the subject
		message.setSubject(sub);


                MimeBodyPart mbp = new MimeBodyPart();
                Multipart mp = new MimeMultipart();
                // set mail format as text/plain �� text/html
                mbp.setContent(body, "text/html;charset=utf-8");
                mp.addBodyPart(mbp);

                FileDataSource fds = new FileDataSource(realfilename);
                BodyPart fileBodyPart = new MimeBodyPart();
                fileBodyPart.setDataHandler(new DataHandler(fds));
                fileBodyPart.setFileName(displayname);
		mp.addBodyPart(fileBodyPart);

                message.setContent(mp);
		message.saveChanges();

		// Send message
		Transport.send(message);
	}

	public static void sendMail(String host, String from, String to,
			String sub, String body, String filename)
			throws javax.mail.internet.AddressException,
			javax.mail.MessagingException {
		sendMail(host, from, to, sub, body, filename, filename);
	}

	public static void sendMail(String host, String from, String to,
			String sub, String body)
			throws javax.mail.internet.AddressException,
			javax.mail.MessagingException {
		boolean debug = false;
		if (System.getProperty("debug") != null)
			debug = true;

		// Get system properties
		Properties sysProps = System.getProperties();

		// Setup mail server
		sysProps.put("mail.debug", "true");
		// sysProps.put("mail.debug","false");
		sysProps.put("mail.smtp.host", host);
		sysProps.put("mail.from", from);

		// Get session
		Session session = Session.getDefaultInstance(sysProps, null);

		// Define message
		MimeMessage message = new MimeMessage(session);

		// Set the from address
		// message.setFrom(new InternetAddress(from));
		if (debug)
			System.out.println(InternetAddress.getLocalAddress(session));
		message.setFrom();

		// Set the to address
		message.addRecipients(Message.RecipientType.TO, InternetAddress
				.parse(to));
		// Set the subject
		message.setSubject(sub);
		message.setText(body);
		message.saveChanges();

		// Send message
		Transport.send(message);
	}

	/*
	 * add by Ethan.
	 */

	public static void sendHTMLMail(String host, String replyTo, String to,
			String sub, String body, String from, String password, String aicentJPGpath, String aicentJPGBottomPath)
			throws javax.mail.internet.AddressException,
			javax.mail.MessagingException {
		boolean debug = false;
		if (System.getProperty("debug") != null)
			debug = true;

		Properties sysProps = System.getProperties();

		// Setup mail server
		sysProps.put("mail.debug", "true");
		sysProps.put("mail.smtp.host", host);
		sysProps.put("mail.from", replyTo);
		sysProps.put("mail.smtp.auth", "true");
		sysProps.put("mail.smtp.user", from);
		sysProps.put("mail.smtp.password", password);

		// Get session
		MyAuthenticator myauth = new MyAuthenticator(from, password);
		Session session = Session.getInstance(sysProps, myauth);

		// Define message
		MimeMessage message = new MimeMessage(session);

		// Set the to address
		message.addRecipients(Message.RecipientType.TO, InternetAddress
				.parse(to));

		// Set the subject
		message.setSubject(sub);

		Multipart mp = new MimeMultipart();
		MimeBodyPart mbp = new MimeBodyPart();

		mbp.setContent(body, "text/html;charset=utf-8");
		mp.addBodyPart(mbp);


			FileDataSource fds = new FileDataSource(aicentJPGpath);
			BodyPart fileBodyPart = new MimeBodyPart();
			fileBodyPart.setFileName("aicent.jpg");
			fileBodyPart.setDataHandler(new DataHandler(fds));
			fileBodyPart.setHeader("Content-ID", "<aicentjpg>");
			mp.addBodyPart(fileBodyPart);

			FileDataSource fds2 = new FileDataSource(aicentJPGBottomPath);
			BodyPart fileBodyPart2 = new MimeBodyPart();
			fileBodyPart2.setFileName("bottom.jpg");
			fileBodyPart2.setDataHandler(new DataHandler(fds2));
			fileBodyPart2.setHeader("Content-ID", "<bottomjpg>");
			mp.addBodyPart(fileBodyPart2);

		message.setContent(mp);

		message.saveChanges();
		// Send message
		Transport.send(message);
	}

}

class MyAuthenticator extends javax.mail.Authenticator {
	private String strUser;
	private String strPwd;

	public MyAuthenticator(String user, String password) {
		this.strUser = user;
		this.strPwd = password;
	}

	protected PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication(strUser, strPwd);
	}
}
